
from flask import jsonify, request
from src.api.controllers.football_controller import FootballController
from src.api.controllers.text_editor_controller import TextEditorController
from src.utils.logger import log_error, log_info


def register_football_routes(bp):
    controller = FootballController()
    text_editor_controller = TextEditorController()

    @bp.route('/football/subtitle', methods=['GET'])
    def get_text_content():
        """获取指定文件的文本内容"""
        file_id = request.args.get('file_id')
        
        try:    
            # 获取文件内容
            result = text_editor_controller.get_subtitle_content(file_id)
            
            return {"success": True, "message": "获取文本内容成功", "data": result}, 200
        except FileNotFoundError as fnf_error:
            return {"success": False, "message": str(fnf_error)}, 404
        except PermissionError as pe:
            return {"success": False, "message": str(pe)}, 403
        except UnicodeDecodeError as ude_error:
            return {"success": False, "message": str(ude_error) }, 400
        except Exception as e:
            return {"success": False, "message": str(e)}, 500
        
    
    @bp.route('/football/subtitle', methods=['PUT'])
    def save_text_content():
        """保存文本内容到指定文件"""
        data = request.json
        
        file_id = data.get('file_id')
        content = data.get('content')
        
        try:
            # 保存文件内容
            result = text_editor_controller.save_subtitle_content(file_id, content)
            return {"success": True, "message": "保存文本内容成功", "data": result}, 200
        except PermissionError as pe:
            return {"success": False, "message": str(pe)}, 403
        except Exception as e:
            return {"success": False, "message": str(e)}, 500
        
    @bp.route('/football/football_icon', methods=['POST'])
    def upload_football_icon():
        """处理足球图标上传请求"""
        if 'icon_file' not in request.files:
            return jsonify({"success": False, "message": "未提供图标文件"}), 400
        if 'icon_type' not in request.form:
            return jsonify({"success": False, "message": "未提供图标类型"}), 400
        if 'icon_filename' not in request.form:
            return jsonify({"success": False, "message": "未提供图标文件名"}), 400
        if 'icon_fileext' not in request.form:
            return jsonify({"success": False, "message": "未提供图标文件扩展名"}), 400
        
        icon_file = request.files['icon_file']
        icon_type = request.form.get('icon_type')
        icon_filename = request.form.get('icon_filename')
        icon_fileext = request.form.get('icon_fileext')
        # 处理文件上传
        result = controller.upload_football_icons(icon_file, icon_type, icon_filename, icon_fileext)
        return jsonify(result)
    
    @bp.route('/football/football_icon', methods=['GET'])
    def get_football_icons():
        """获取足球图标列表"""
        icon_type = request.args.get('icon_type')
        if not icon_type:
            return jsonify({"success": False, "message": "未提供图标类型"}), 400
        page = request.args.get('page', 1, type=int)
        page_size = request.args.get('page_size', 10, type=int)
        log_info(f"获取足球图标: 类型={icon_type}, 页码={page}, 每页数量={page_size}")
        try:
            result = controller.get_football_icons(icon_type, page, page_size)
            log_info(f"获取足球图标结果: {result}")
            return {"success": True, 
                    "data": result,
                    "message": "获取足球图标成功"
                    }, 200
        except Exception as e:
            log_error(f"获取足球图标时出错: {e}", include_stack=True)
            return {"success": False, "message": str(e)}, 500
        
        
    @bp.route('/football/football_icon/<icon_id>', methods=['DELETE'])
    def delete_football_icon(icon_id):
        """删除指定类型的足球图标"""
        if not icon_id:
            return jsonify({"success": False, "message": "未提供图标类型"}), 400
        
        try:
            result = controller.delete_football_icon(icon_id)
            return jsonify(result)
        except Exception as e:
            return jsonify({"success": False, "message": str(e)}), 500
        
    @bp.route('/football/football_icon/<icon_id>', methods=['PUT'])
    def update_football_icon_filename(icon_id):
        """更新足球图标文件名"""
        data = request.json
        icon_filename = data.get('icon_filename')
        if not icon_id or not icon_filename:
            return jsonify({"success": False, "message": "未提供图标ID或图标文件名"}), 400
        
        try:
            result = controller.update_football_icon_filename(icon_id, icon_filename)
            return {"success": True, "message": "更新足球图标文件名成功", "data": result}, 200
        except Exception as e:
            return jsonify({"success": False, "message": str(e)}), 500
        
    @bp.route('/football/icon_videos', methods=['POST'])
    def generate_icon_videos():
        """生成足球图标视频"""
        data = request.json
        
        subtitle_id = data.get('subtitle_id')
        video_paths = data.get('video_paths', [])
        if not subtitle_id or not video_paths:
            return {"success": False, "message": "未提供字幕文件名或视频路径"}, 400
        
        try:
            result = controller.generate_icon_videos(video_paths, subtitle_id)
            return {"success": True, "message": "生成足球图标视频成功", "data": result}, 200
        except FileNotFoundError as fnf_error:
            return {"success": False, "message": str(fnf_error)}, 404
        except PermissionError as pe:
            return {"success": False, "message": str(pe)}, 403
        except ValueError as ve:
            return {"success": False, "message": str(ve)}, 400
        except Exception as e:
            return {"success": False, "message": str(e)}, 500    